using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Localization;
using Orchard.UI.Notify;
using Mod.CookieDetector.Models;

namespace Mod.CookieDetector.Drivers
{
    [UsedImplicitly]
	
    public class CookieDetectionSettingsPartDriver : ContentPartDriver<CookieDetectionSettingsPart>
    {
        private readonly INotifier _notifier;
        private const string TemplateName = "Parts/CookieDetectionSettingsPart";

        public Localizer T { get; set; }

        public CookieDetectionSettingsPartDriver(INotifier notifier)
        {
            _notifier = notifier;
            T = NullLocalizer.Instance;
        }

        protected override DriverResult Display(CookieDetectionSettingsPart part, string displayType, dynamic shapeHelper)
        {
            return ContentShape("Parts_CookieDetectionSettingsPart",
                () => shapeHelper.Parts_CookieDetectionSettingsPart(ContentItem: part.ContentItem));
        }

        protected override DriverResult Editor(CookieDetectionSettingsPart part, dynamic shapeHelper)
        {
            return ContentShape("Parts_CookieDetectionSettingsPart",
                    () => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: part, Prefix: Prefix)).OnGroup("login");
        }

        protected override DriverResult Editor(CookieDetectionSettingsPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            if (updater.TryUpdateModel(part, Prefix, null, null))
            {
                _notifier.Information(T("CookieDetectionSettingsPart edited successfully"));
            }
            else
            {
                _notifier.Error(T("Error during CookieDetectionSettingsPart update!"));
            }
            return Editor(part, shapeHelper);
        }

    }
}